package aishua.com.mytestapplist.engin;

import android.content.Context;

import java.io.IOException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;

import aishua.com.mytestapplist.MyApplication;
import aishua.com.mytestapplist.bean.ReqAppletApduChatBean;
import aishua.com.mytestapplist.bean.ResAppletApduChatBean;
import aishua.com.mytestapplist.coredata.ApduInfo;
import aishua.com.mytestapplist.engin.impl.WebConnection;
import aishua.com.mytestapplist.utils.CTWalletLog;


public class WebServiceConnection {
	private static final String TAG = "WebServiceConnection-";
	private String imsi;
	private String seid;
	
	public static final int MSG_CONNECT_SUCCESS = 100;
	public static final int MSG_CONNECT_FAILED = 101;
	public static final int MSG_CONNECT_KEYSTOR_ERROR = 102;
	public static final int MSG_SERVER_ERROR = 103;
	public static final int MSG_TIME_OUT = 104;
	public static final int MSG_NOTIFY_BANNER = 105;
	public static final int MSG_INIT_CHANNEL = 106;
	public static final int MSG_GET_PPSE_SUCCESS = 107;

	
	public static final int HTTPS_CONNECT_TIME_OUT = 180000;
	public static final int HTTPS_READ_TIME_OUT = 180000;
	

	
	private static WebServiceConnection webServiceConnection;

	public static WebServiceConnection getInstant(Context context) {
		if (webServiceConnection == null) {
			webServiceConnection = new WebServiceConnection();
			String iccid = MyApplication.mSeID;
			webServiceConnection.setImsi(MyApplication.mImsi);
			webServiceConnection.setSeid(iccid);
//			webServiceConnection.mContext = context;
		}
		return webServiceConnection;
	}
	
	public String getSeid() {
		return seid;
	}

	public void setSeid(String seid) {
		this.seid = seid;
	}

	public String getImsi() {
		return imsi;
	}

	public void setImsi(String imsi) {
		this.imsi = imsi;
	}


	/**
	 * 8.2.2	Ӧ������/����/����/ɾ��
	 * @param appAID
	 * @param operType
	 * @param rApduInfoList
	 * @return
	 * @throws IOException
	 */
	public ResAppletApduChatBean doAppletApduChat(String appAID, String appletVersion,
												  String operType, List<ApduInfo> rApduInfoList, short i, String result)
					throws Exception {
		CTWalletLog.print(TAG+"doAppletApduChat()");
		ReqAppletApduChatBean reqBean = new ReqAppletApduChatBean();
		reqBean.setVersion(WebServiceConstant.VERSION_PACKAGE);
		reqBean.setKeyVersion(WebServiceConstant.VERSION_KEY);
		reqBean.setKeyIndex(WebServiceConstant.INDEX_KEY);
		reqBean.setBusinessType(WebServiceConstant.REQ_APPLETAPDUCHAT_TYPE);
		reqBean.setTraceNo("00000000000000000000");
		reqBean.setImsi(getImsi());
		reqBean.setSeid(getSeid());
		reqBean.setAppAID(appAID);
		reqBean.setAppletVersion(appletVersion);
		reqBean.setOsType(WebServiceConstant.OS_TYPE);
		reqBean.setOperType(operType);
		reqBean.setTaskIndex(i);
		reqBean.setResult(result);
		
		reqBean.setImei(MyApplication.mImei);
		reqBean.setPhone(MyApplication.mPhoneType);
		
		reqBean.setResApduInfoList(rApduInfoList);
		String xmlReq = "ClientReq="+reqBean.buildPackage();
		
		String xmlRes = executeHttpsPost(MyApplication.mURL, xmlReq);
		
		if (xmlRes != null) {
			ResAppletApduChatBean resBean = new ResAppletApduChatBean();
			resBean.parserXmlPackage(xmlRes);
			if (("1054014").equals(resBean.getOperationResult())
					|| ("1054011").equals(resBean.getOperationResult())||("1054016").equals(resBean.getOperationResult())) {

				xmlReq = "ClientReq="+reqBean.buildPackage();
				xmlRes = executeHttpsPost(MyApplication.mURL, xmlReq);
				if (xmlRes != null) {
					resBean = new ResAppletApduChatBean();
					resBean.parserXmlPackage(xmlRes);
				}
				
			}
			return resBean;
		}
		return null;
	}

	private String executeHttpsPost(String host, String xmlReq) throws
			NoSuchAlgorithmException, KeyManagementException, Exception {
		CTWalletLog.print(TAG+"executeHttpsPost()");

		WebConnection webConn = new WebConnection(host);
		String result = webConn.executeHttpsPost(host, xmlReq, true);
//        CTWalletLog.print("CTWallet receive message:", result);
        CTWalletLog.print(TAG+"executeHttpsPost()-CTWallet receive message:"+result);
		return result;
	
	}
	
	
	
	
	
	
}
